Higher-order imperative enumeration of binary trees in COQ
نویسنده
چکیده
Inductive bintree : Set := | Leaf: bintree | Node: bintree -> bintree -> bintree. In the following, I also use some definitions of the Coq library. Type nat is type of Peano numbers generated from O and S. Type Z is type of infinite binary integers (more efficient than nat to perform large concrete computations). At last, list is the polymorphic type of lists. The Coq definition of enumBT is given below. Here, K is the “specification type” of the state DSM (see [Bou06]). It is parametrized both by St the type of the global state and by unit the type of the result. It uses an infix operator -; to denote sequences: “p1 -; p2” is a notation for “bind p1 (fun :unit => p2)”. The main advantage of this CPS-like implementation is to call f as soon as a tree is computed, before to compute the next tree. Moreover, whereas the number of binary trees is exponential in function of 2n (e.g. the number of nodes of a balanced binary tree of height n), this function requires only a memory linear in function of 2n. Fixpoint enumBT (St:Type) (n:nat) (f:bintree -> K St unit) {struct n} : K St unit := match n with | O => f Leaf | (S p) => enumBT p (fun l => enumBT p (fun r => f (Node l r)) -; enumlt p (fun r => f (Node l r) -; f (Node r l))) end with enumlt (St:Type) (n:nat) (f:bintree -> K St unit) {struct n} : K St unit := match n with | O => skip | (S p) => (enumBT p f) -; (enumlt p f) end.
منابع مشابه
ModuRes: A Coq Library for Modular Reasoning About Concurrent Higher-Order Imperative Programming Languages
It is well-known that it is challenging to build semantic models of type systems or logics for reasoning about concurrent higher-order imperative programming languages. One of the key challenges is that such semantic models often involve constructing solutions to certain kinds of recursive domain equations, which in practice has been a barrier to formalization efforts. Here we present the ModuR...
متن کاملGrammar-Oriented Enumeration of Binary Trees
In contrast to traditional integer sequences for the representation of binary trees, a kind of character sequence (words) is presented for binary trees based on a grammar GBT and for full binary trees based on a grammar GFBT. The properties of words derived from GBT (GFBT) are discussed in depth, including necessary and suf"cient conditions for a word, pre"x and suf"x of &(GBT) (&(GFBT)) and al...
متن کاملFormalizing and Verifying Semantic Type Soundness of a Simple Compiler (Preliminary Report)
We describe a semantic type soundness result, formalized in the Coq proof assistant, for a compiler from a simple imperative language with heap-allocated data into an idealized assembly language. Types in the high-level language are interpreted as binary relations, built using both second-order quantification and a form of separation structure, over stores and code pointers in the low-level mac...
متن کاملPointer Program Derivation Using Coq: Graphs and Schorr-Waite Algorithm
We present a specification, a derivation and total correctness proofs of operations for bi-functional graphs implemented with pointers, including the Schorr-Waite algorithm. This one marks such a graph with an economical depth-first strategy. Our approach is purely algebraic and functional, from a simple graph specification to the simulation of a tailrecursive imperative program, then to a true...
متن کاملOn Embedded Trees and Lattice Paths
Bouttier, Di Francesco and Guitter introduced a method for solving certain classes of algebraic recurrence relations arising the context of embedded trees and map enumeration. The aim of this note is to apply this method to three problems. First, we discuss a general family of embedded binary trees, trying to unify and summarize several enumeration results for binary tree families, and also to ...
متن کامل